/*
 * FilterJPanel.java
 *
 * Created on 05.04.2009, 0:30:25
 */
package morenoapp.components.thinkgraph;

import com.l2fprod.common.swing.JTaskPane;
import com.l2fprod.common.swing.JTaskPaneGroup;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JLabel;
import morenoapp.MorenoApp;
import morenoapp.util.MorenoUtil;
import morenoapp.components.common.VerticalLayout;
import morenoapp.components.thinkgraph.model.DisplayFilter;
import morenoapp.components.thinkgraph.model.DisplayMode;
import morenoapp.model.data.MorenoModelFace;
import morenoapp.model.data.MorenoQuestionFace;
import morenoapp.util.I18N;

/**
 * 
 * @author dmitry.mamonov
 */
public class ThinkGraphDisplayFilterJPanel extends javax.swing.JPanel {

    DisplayFilter filter;
    JTaskPane jTaskPane = new JTaskPane();

    /** Creates new form FilterJPanel */
    public ThinkGraphDisplayFilterJPanel(DisplayFilter _filter) {
        this.filter = _filter;

        initComponents();
        setLayout(new BorderLayout(0,0));
        add(jTaskPane);
    }

    private PartCommonFilterJPanel createCommonFilter() {
        return new PartCommonFilterJPanel(filter);
    }

    private JPanel createNodeFilter() {
        //Фитр отображения для узлов
        JPanel nodesFilterPanel = new JPanel(new VerticalLayout(0, VerticalLayout.LEFT));
        //nodesFilterPanel.setBorder(new TitledBorder("Фильтр по узлам графа:"));
        nodesFilterPanel.add(new PartNodesFilterJPanel(filter));
        nodesFilterPanel.add(new JLabel(I18N.get("label.graph.add_filter_by_nodes")));
        return nodesFilterPanel;
    }

    private JPanel createRelationshipFilter(MorenoModelFace model) {
        //Фильтр по связям
        JPanel questionsFilterPanel = new JPanel(new VerticalLayout(0, VerticalLayout.LEFT));
        //questionsFilterPanel.setBorder(new TitledBorder("Отображать связи по вопросам:"));
        questionsFilterPanel.add(new PartEdgesFilterJPanel());
        if (model.getQuestionsList().size() > 0) {
            questionsFilterPanel.add(new JLabel(I18N.get("label.graph.display_filter_by_questions_colon")));
            for (final MorenoQuestionFace question : model.getQuestionsList()) {
                final JCheckBox checkBox = new JCheckBox();
                checkBox.setSelected(filter.getDisplayModeForQuestion(question) == DisplayMode.Normal);
                checkBox.setText("<html><font color='" + MorenoUtil.colorToHtml(question.getColor()) + "'>" + MorenoUtil.limitString(question.toPlainString(), 35) + "</font>"); //NOI18N
                checkBox.setToolTipText("+1" + question.toPlainString()); //NOI18N
                checkBox.addActionListener(new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        filter.setDisplayModeForQuestion(question, checkBox.isSelected() ? DisplayMode.Normal : DisplayMode.Gray);
                    }
                });
                questionsFilterPanel.add(checkBox);
            }
        } else {
            questionsFilterPanel.add(new JLabel(I18N.get("label.graph.no_question_specified_in_model")));
        }
        questionsFilterPanel.doLayout();
        return questionsFilterPanel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    public void refreshFilter() {
        jTaskPane.removeAll();
        jTaskPane.setBackground(Color.GRAY);

        //common filter
        {
            JTaskPaneGroup group = new JTaskPaneGroup();
            group.setTitle(I18N.get("label.graph.common_parameters"));
            group.setToolTipText(null);
            group.setSpecial(false);
            group.setIcon(null);
            group.add(createCommonFilter());
            jTaskPane.add(group);
        }

        //nodes filter
        {
            JTaskPaneGroup group = new JTaskPaneGroup();
            group.setTitle(I18N.get("label.graph.filter_by_graph_nodes"));
            group.setToolTipText(null);
            group.setSpecial(false);
            group.setIcon(null);
            group.add(createNodeFilter());
            jTaskPane.add(group);
        }

        //edges filter
        {
            JTaskPaneGroup group = new JTaskPaneGroup();
            group.setTitle(I18N.get("label.graph.filter_by_relationships"));
            group.setToolTipText(null);
            group.setSpecial(false);
            group.setIcon(null);
            group.add(createRelationshipFilter(MorenoApp.getApplicationData().getMorenoModelProxy()));
            jTaskPane.add(group);
        }
        revalidate();
        repaint();
    }
}
